package com.instwall.callnumlistener;

import android.os.RemoteCallbackList;
import android.util.Log;

import com.instwall.callbase.thread.BasicThread;
import com.instwall.callnumlistener.task.AddClientTask;
import com.instwall.callnumlistener.task.NotificationTask;
import com.instwall.callnumlistener.task.RemoveClientTask;
import com.instwall.callnumserver.ICallNumListener;

import ashy.earl.common.util.ModifyList;

public class CallNumNotification {
    public static RemoteCallbackList<ICallNumListener> mListeners = new RemoteCallbackList<>();
    public static ModifyList<PlayerStateListener>  playerStateListeners = new ModifyList<>();
    private static CallNumNotification callNumNotification;
    private PlayerStateListener playerStateListener;
    private BasicThread thread = new BasicThread("CallNum_Notification");
    private String TAG = CallNumNotification.class.getName();


    public static CallNumNotification getCallNumNotification() {
        if (callNumNotification == null)
            callNumNotification = new CallNumNotification();
        return callNumNotification;
    }

    private CallNumNotification() {
        thread.start();
    }


    public void addClientClientListener(ICallNumListener listener) {
        thread.postTask(new AddClientTask(listener));
        Log.d(TAG, "addClientClientListener: ");
        onPlayerChanged();
    }

    public void removeClientListener(ICallNumListener listenere) {
        thread.postTask(new RemoveClientTask(listenere));
    }

    public void notificationClient(String data) {
        thread.postTask(new NotificationTask(data));
    }

    public void onPlayerChanged(){
        if(playerStateListeners.size()==0) return;
        if (playerStateListeners!=null){

            for (PlayerStateListener listener : playerStateListeners) {
                listener.onPlayerStateChanged();
            }
        }
    }
    public void addPlayerStateListener(PlayerStateListener listener) {
        playerStateListeners.add(listener);
    }

    public void removePlayerStateListener(PlayerStateListener listener) {
        playerStateListeners.remove(listener);
    }


    public interface PlayerStateListener{
        void onPlayerStateChanged();
    }
}
